<?php

namespace common\helpers;

use Yii;
use yii\queue\JobInterface;
use common\jobs\TradeExpiredJob;
use common\models\shop\ShopOrderServiceApply;
use common\jobs\ServiceWorkflowJob;
use common\jobs\notice\BusinessNotifyJob;

/**
 * 队列扩展类
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class QueueHelper {

    /**
     * 创建服务流程队列
     * @param ShopOrderServiceApply $apply
     * @param ShopServiceSupportWorkflow $workflow
     * @param integer|null $delay
     */
    public static function workflow($apply, $workflow, $delay = null) {
        if (!$workflow) {
            Yii::error([
                'message' => 'Workflow undefind.',
                'data' => $apply->attributes,
            ], __METHOD__);
        }
        if (!Yii::$app->has('queue')) {
            Yii::error([
                'message' => 'Workflow Queue undefind.',
                'data' => $workflow->attributes,
            ], __METHOD__);
            return false;
        }
        $queue = Yii::$app->queue;
        if ($delay) {
            $queue->delay($delay);
        }
        $params = [
            'class' => ServiceWorkflowJob::class,
            'options' => [
                'modelClass' => $apply::className(),
                'model_id' => $apply->id,
                'workflowClass' => $workflow::className(),
                'workflow_id' => $workflow->id,
            ]
        ];
        Yii::error([
            'message' => 'Queue job $params.',
            'data' => $params,
        ], __METHOD__);
        $job = Yii::createObject($params);
        $taskId = $queue->push($job);
        Yii::error([
            'message' => 'Queue is created.',
            'taskId' => $taskId,
            'data' => $params,
        ], __METHOD__);
        return $taskId;
    }

    /**
     * 创建交易过期队列
     * @param JobInterface|array $data
     * @param integer $delay
     */
    public static function expireTrade($data, $delay = null) {
        if (!Yii::$app->has('queue')) {
            Yii::error([
                'message' => 'Queue undefind.',
                'data' => $data,
            ], __METHOD__);
            return false;
        }
        $queue = Yii::$app->queue;
        if ($delay) {
            $queue->delay($delay);
        }
        $job = $data;
        if (is_array($data)) {
            if (!isset($data['class'])) {
                $data['class'] = TradeExpiredJob::class;
            }
            $job = Yii::createObject($data);
        }
        if (!$job instanceof JobInterface) {
            Yii::error([
                'message' => 'Queue Job failed.',
                'data' => $data,
            ], __METHOD__);
            return false;
        }
        $taskId = $queue->push($job);
        Yii::error([
            'message' => 'Queue is created.',
            'taskId' => $taskId,
            'data' => $data,
        ], __METHOD__);

        //自动支付
//        static::autoPay($data);

        return $taskId;
    }

    /**
     * 延迟推送任务
     * @param integer $id
     * @return type
     */
    public static function businessNotify($id, $delay = null) {
        if (!$id) {
            Yii::error([
                'message' => 'Trade Id undefind.',
                'data' => $id,
            ], __METHOD__);
        }
        if (!Yii::$app->has('queue')) {
            Yii::error([
                'message' => 'Trade Queue undefind.',
                'data' => $id,
            ], __METHOD__);
            return false;
        }
        $queue = Yii::$app->queue;
        if ($delay) {
            $queue->delay($delay);
        }
        $params = [
            'class' => BusinessNotifyJob::class,
            'trade_id' => $id,
        ];
        Yii::error([
            'message' => 'BusinessNotify Queue job attributes.',
            'data' => $params,
        ], __METHOD__);
        $job = Yii::createObject($params);
        $taskId = $queue->push($job);
        Yii::error([
            'message' => 'BusinessNotify Queue is created.',
            'taskId' => $taskId,
            'data' => $params,
        ], __METHOD__);
        return $taskId;
    }

    /**
     * 测试环境模拟支付
     * @param type $data
     * @return type
     */
    public static function autoPay($data) {
        if (!ArrayHelper::getValue(Yii::$app->params, 'debug.trade.autopay')) {
            return;
        }
        $notify = new \common\mocks\PaymentMockNotify();
        $notify->trade_id = ArrayHelper::getValue($data, 'tradeid');
        $notify->payment();
    }

}
